package gao.xiaolei.config;

import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import xiaolei.gao.Image.ImageInterface;
import xiaolei.gao.file.FileInterface;
import xiaolei.gao.git.IGit;
import javax.servlet.MultipartConfigElement;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Arrays;

@Configuration
public class ConfigBean {
    @Bean
    MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        String location = System.getProperty("user.dir") + "/data/tmp";
        File tmpFile = new File(location);
        if (!tmpFile.exists()) {
            tmpFile.mkdirs();
        }
        factory.setLocation(location);
        return factory.createMultipartConfig();
    }

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
        // RestTemplate restTemplate = new RestTemplate();
//        //换上fastjson
//        List<HttpMessageConverter<?>> messageConverters= restTemplate.getMessageConverters();
//        Iterator<HttpMessageConverter<?>> iterator=messageConverters.iterator();
//        while(iterator.hasNext()){
//            HttpMessageConverter<?> converter=iterator.next();
//            //原有的String是ISO-8859-1编码 去掉
//            if(converter instanceof StringHttpMessageConverter){
//                iterator.remove();
//            }
//            //由于系统中默认有jackson 在转换json时自动会启用  但是我们不想使用它 可以直接移除
//            if(converter instanceof GsonHttpMessageConverter || converter instanceof MappingJackson2HttpMessageConverter){
//                iterator.remove();
//            }
//        }
//        messageConverters.add(new StringHttpMessageConverter(Charset.forName("utf-8")));
//        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
//        FastJsonConfig fastJsonConfig = new FastJsonConfig();
//        fastJsonConfig.setSerializerFeatures(
//                SerializerFeature.WriteMapNullValue,
//                SerializerFeature.WriteNullStringAsEmpty,
//                SerializerFeature.WriteNullListAsEmpty,
//                SerializerFeature.DisableCircularReferenceDetect);
//        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
//        messageConverters.add(fastJsonHttpMessageConverter);
//        return restTemplate;
    }

    @Bean("FileInterface")
    @Autowired
    public FileInterface fileInterface(RestTemplate restTemplate) {
        return new FileInterface(restTemplate);
    }

    @Bean("ImageInterface")
    @Autowired
    public ImageInterface imageInterface(RestTemplate restTemplate) {
        return new ImageInterface(restTemplate);
    }

    @Bean("GitInterface")
    @Autowired
    public IGit gitInterface(RestTemplate restTemplate) {
        return new IGit(restTemplate);
    }

    @Bean("GsonUtil")
    public Gson getGson() {
        return new Gson();
    }

    @Bean
    public MultipartResolver multipartResolver() {//如果不配置这个的话springboot就不支持put形式的form-data请求
        return new StandardServletMultipartResolver() {
            @Override
            public boolean isMultipart(HttpServletRequest request) {
                String method = request.getMethod().toLowerCase();
                //By default, only POST is allowed. Since this is an 'update' we should accept PUT.
                if (!Arrays.asList("put", "post").contains(method)) {
                    return false;
                }
                String contentType = request.getContentType();
                return (contentType != null && contentType.toLowerCase().startsWith("multipart/"));
            }
        };
    }
}
